/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef TRI_EXT_COMPONENT_H_
#define TRI_EXT_COMPONENT_H_

#include <iostream>
#include <vector>
#include <string>
#include <map>

//Include library header
#include "../texture.h"
#include "../glyph.h"

#include "style.h"

namespace tri {
namespace ui {

class extDimensions {
public:
	extDimensions(float w=0, float h=0);
	float w;
	float h;

	void limitMax(extDimensions* d);
	void limitMin(extDimensions* d);
};

/**
 * Get a texture by the given name. If the texture does not 
 * exists the function returns NULL.
 * @return Reference tp a texture in memory. NULL if texture does not exists.
 */
tri::TTexture* getTexture(std::string _image);

//======================================================================

/**
 * Component class used for user interface
 */
class extComponent : public tri::TEntity {
public:
	extComponent();
	virtual ~extComponent();

	/**
	 * Reimplemented to do nothing
	 */
	void addEntity(TEntity* e);

	/**
	 * @see entity::addEntity
	 */
	void addComponent(extComponent* c);

	/**
	 * Display Object
	 */
	virtual void update();

	virtual void display();

	void setImage(std::string im);
	void setColor(int r, int g, int b, int a);
	void setColor(tri::graphics::TColor c);
	virtual bool childHasFocus();
	/**
	 * Apply the current State
	 */
	virtual bool updateStyle();
	
	void getComponents(std::vector<extComponent*> &list);

	virtual void applyStyle(int event);

	/**
	 * Set the components style
	 */
	virtual void setStyle(std::string name);

	/**
	 * Refresh child component layout
	 */
	virtual void pack();

	/**
	 * Get the components style
	 */
	std::string getStyle();

	/**
	 * Set the segment insets
	 */
	void setSegmentInsets(float l, float t, float r, float b);

	void setImageInsets(float l, float t, float r, float b);
	void setInsets(float l, float t, float r, float b);
	void setClipInsets(float l, float t, float r, float b);

	void setSegmentInsets(tri::TRectangle*);
	void setImageInsets(tri::TRectangle*);
	void setInsets(tri::TRectangle*);

	bool isHidden();

	//void setMinimumSize(float w,float h);
	//void setPreferredSize(float w, float h);
	//void setMaximumSize(float w,float h);

	//extDimensions getSize();
	//void setSize(extDimensions d);

	extDimensions getMinimumSize();
	extDimensions getPrefferedSize();
	extDimensions getMaximumSize();

	tri::TRectangle getInsets();
	tri::TRectangle getClipInsets();

	void setOverlayDistance(float distance);
	float getOverlayDistance();

private:

	/**
	 * Update the components
	 */
	void updateChildComponents();

	//Components style object
	std::string mStyle;

	tri::TGlyph mTopSegment;
	tri::TGlyph mTopLeftSegment;
	tri::TGlyph mTopRightSegment;
	tri::TGlyph mLeftSegment;
	tri::TGlyph mCenterSegment;
	tri::TGlyph mRightSegment;
	tri::TGlyph mBottomSegment;
	tri::TGlyph mBottomLeftSegment;
	tri::TGlyph mBottomRightSegment;

	tri::TRectangle mSegmentInsets;
	tri::TRectangle mImageInsets;
	tri::TRectangle mInsets;
	tri::TRectangle mClipInsets;

	float mOverlayDistance;

	//extDimensions mPreferredSize;
	//extDimensions mMaximumSize;
	//extDimensions mMinimumSize;

protected:
	//Current State
	int mLastState;
	bool mFocusState;
	int mCurrentStyleState;
};
}
}

#endif
